package com.longrise.android.byjk.plugins.aboutme.personalInfo.product_show;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.android.byjk.R;
import com.longrise.android.byjk.event.PersonalInfoEvent;
import com.longrise.android.byjk.model.UserInfor;
import com.longrise.common.base.BaseActivity2;
import com.longrise.common.utils.AppUtil;
import com.longrise.common.utils.PrintLog;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/3/9.
 * 产品方案---列表页
 */

public class ProductProgramActivity extends BaseActivity2<ProductProgramPresenter> implements ProductProgramContract.View, View.OnClickListener
                                ,ProductProAdapter.OnItemClickListener{


    public static final String TAG = "ProductProgramActivity";
    private RecyclerView mRcv;
    private Button mBtn;
    private ProductProAdapter mAdapter;
    private LinearLayout mllProductNull;
    private boolean isChecked;
    private List<String> productIds;
    private RelativeLayout mRlProduct;
    private Button mBtnAdd;
    private List<ProductProBean> mSelectedBeans = new ArrayList<>();
    private List<ProductProBean> mProductList ;
    private boolean isEdit = false;


    @Override
    public int getContentViewId(@Nullable Bundle savedInstanceState) {

        return R.layout.activity_product_program;
    }

    @Override
    public void initView() {
        mRlProduct = (RelativeLayout) findViewById(R.id.rl_product_rcv);
        mRcv = (RecyclerView) findViewById(R.id.rcv_Product_program);
        mBtn = (Button) findViewById(R.id.btn_product_add_again);
        mBtnAdd = (Button) findViewById(R.id.btn_product_add);
        mllProductNull= (LinearLayout) findViewById(R.id.ll_product_pro_null);
        EventBus.getDefault().register(this);
        setToolbarTitle(AppUtil.getString(R.string.cpzs));
        initRcv();
        initEvent();
    }

    @Override
    public void onToolbarBackClick() {
        finish();
        PersonalInfoEvent event = new PersonalInfoEvent(false);
        event.setmIsUpdateMyMedal(true);
        EventBus.getDefault().post(event);
    }

    private void initRcv() {
        View footerView = View.inflate(mContext, R.layout.item_product_pro_footer, null);
        mAdapter = new ProductProAdapter();
        mAdapter.addFooterView(footerView);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        mRcv.setLayoutManager(layoutManager);
        mRcv.setAdapter(mAdapter);
        mPresenter.getProductProData();
    }

    public void initEvent(){
        mBtn.setOnClickListener(this);
        mBtnAdd.setOnClickListener(this);
        mAdapter.setOnItemClickListener(this);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){

            //点击马上添加
            case R.id.btn_product_add:
                startActivity(AddProductProActivity.class);
                break;

            //点击继续添加
            case R.id.btn_product_add_again:
                startActivity(AddProductProActivity.class);
                break;

        }
    }


    @Override
    public void refreshData(List<ProductProBean> mProductList) {
        this.mProductList = mProductList;
        PrintLog.e(TAG,"mProductList=:"+mProductList.toString());
        if(mProductList.size() != 0){
            mAdapter.setNewData(mProductList);
            mRlProduct.setVisibility(View.VISIBLE);
            mllProductNull.setVisibility(View.GONE);
            if(mProductList.size()>=3){
                mBtn.setVisibility(View.GONE);
            }else{
                mBtn.setVisibility(View.VISIBLE);
            }
            UserInfor.getInstance().setUserCPZS(mProductList.size()+"个");
            setToolbarRightTextTitle(AppUtil.getString(R.string.person_honor_edit));
        }else {
            mllProductNull.setVisibility(View.VISIBLE);
            mRlProduct.setVisibility(View.GONE);
            mAdapter.setNewData(null);
            setToolbarRightTextTitle("");
            UserInfor.getInstance().setUserCPZS("未填写");
        }
    }

    @Override
    public void responseDelProduct(EntityBean bean) {
    }

    @Override
    public void onToolbarRightTvClick() {
        super.onToolbarRightTvClick();
        startActivity(ProductProEditActivity.class);
    }

    @Override
    public void onItemClick(ProductProBean productBean) {
        String productId = productBean.getProductId();
        String productName = productBean.getProductName();
        String productdesc = productBean.getProductdesc();
        //跳转到个人荣誉编辑页面
        Intent intent = new Intent(this,AddProductProActivity.class);
        intent.putExtra(AddProductProActivity.PRODUCTID,productId);
        intent.putExtra(AddProductProActivity.PRODUCTNAME,productName);
        intent.putExtra(AddProductProActivity.PRODUCTDESC,productdesc);
        startActivity(intent);

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void refreshProductEvent(ProductProEvent event) {
        if (event.isRefresh()) {
            mPresenter.getProductProData();
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode == KeyEvent.KEYCODE_BACK){
            finish();
            PersonalInfoEvent event1 = new PersonalInfoEvent(false);
            event1.setmIsUpdateMyMedal(true);
            EventBus.getDefault().post(event1);
            return false;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
